
package com.hxl.demo.controller;

import com.hxl.demo.entity.Dictionary;
import com.hxl.demo.entity.SocialSecurityCard;
import com.hxl.demo.service.DictionaryService;
import com.hxl.demo.service.SocialSecurityCardService;
import com.hxl.demo.tools.PageInfo;
import com.hxl.demo.tools.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.lang.reflect.Parameter;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class SocialSecurityCardController {
    @Resource
    private SocialSecurityCardService socialSecurityCardService;
    @Resource
    private DictionaryService dictionaryService;
    @PostMapping("/selectAllSocialCard")
    public Result selectAllSocialCard(@Valid SocialSecurityCard socialSecurityCard
                                        , @RequestParam(required = false) String begin
                                        , @RequestParam(required = false) String length) {
        int start = (begin == null) ? 0 : Integer.parseInt(begin);
        int limit = (length == null) ? 15 : Integer.parseInt(length);
        if (start > 0) {
            start = (start - 1) * limit;
        } else {
            start = 0;
        }
        PageInfo<SocialSecurityCard> pageInfo = socialSecurityCardService.selectAll(start, limit, socialSecurityCard);
        if (pageInfo != null) {
            return Result.ok().data("list", pageInfo);
        }else {
            return Result.fail();
        }
    }

    @GetMapping("/selectAllDictName")
    public Result selectAllDictName() {
        List<Dictionary> dictionaries = dictionaryService.selectDictName();
        if(dictionaries != null) {
            return Result.ok().data("dictName", dictionaries);
        } else {
           return Result.fail();
        }
    }
    @PostMapping("/updateById")
    public Result updateById(@Valid SocialSecurityCard socialSecurityCard) {
        //会有前端传递的时间少一天的情况，可能是版本问题,开始日期和弃买日期都是
        System.out.println(socialSecurityCard.getPurchaseDate());
        System.out.println(socialSecurityCard.getAbandonBuyingDate());
        int number = socialSecurityCardService.updateById(socialSecurityCard);

        if(number > 0) {
            return Result.ok();
        }  else {
            return Result.fail();
        }
    }

    @PostMapping("/saveSocialCard")
    public Result saveSocial(@Valid SocialSecurityCard socialSecurityCard, @RequestParam(value="tel") String tel) {
        //通过电话号码确定员工的唯一性，然后再添加
        int number = socialSecurityCardService.saveSocial(socialSecurityCard, tel);
        //身份证没查询到员工
        if(number == -1) {
            return Result.fail().message("卡号重复或查无此人！");
        } else if (number > 0) {
            //成功
            return Result.ok();
        } else {
            return Result.fail().message("添加失败！");
        }
    }
}

